package team.fjut.cf.controller;


import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import team.fjut.cf.config.interceptor.annotation.LoginRequired;
import team.fjut.cf.pojo.enums.ResultCode;
import team.fjut.cf.pojo.po.ProblemAanalysisPO;
import team.fjut.cf.pojo.vo.ProblemAnalysisPreviewVO;
import team.fjut.cf.pojo.vo.ResultJson;
import team.fjut.cf.pojo.vo.UserCustomInfoVO;
import team.fjut.cf.service.ProblemAnalysisService;
import team.fjut.cf.service.UserCustomInfoService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
//@RestController的作用等同于@Controller + @ResponseBody
//添加@Controller注解，表明了这个类是一个控制器类
//@ResponseBody表示方法的返回值直接以指定的格式写入Http response body中，而不是解析为跳转路径
@RequestMapping("/problem/analysis")
//地址映射父路径
@CrossOrigin
//加入@CrossOrigin即可解决跨域问题
public class ProblemAnalysisController {
    //装备bean
    @Resource
    ProblemAnalysisService problemAnalysisService;


    @Resource
    UserCustomInfoService userCustomInfoService;


    //添加题解

    @LoginRequired
    //必须处于登录状态才能访问
    @PostMapping("/add")
    //映射子路径
    //@RequestParam,把请求中的指定名称的参数传递给控制器中的形参赋值
    public ResultJson addProblemAnalysis(@RequestParam("username") String username,
                                         @RequestParam("title") String title,
                                         @RequestParam("content") String content,
                                         @RequestParam("problemId") Integer problemId
                                         ){
        ResultJson resultJson = new ResultJson();
        int result = problemAnalysisService.insertProblemAnalysis(title,content,username,problemId);
        if(result == 1)
            resultJson.setStatus(ResultCode.REQUIRED_SUCCESS);
        return resultJson;
    }


    //根据题目返回题解预览
    //@LoginRequired
    @PostMapping("/preview")
    public ResultJson selectProblemAnalysisPreview(@RequestParam("pageNum") Integer pageNum,
                                                   @RequestParam("pageSize") Integer pageSize,
                                                   @RequestParam(value = "problemId",required = false) Integer problemId,
                                                   @RequestParam(value = "titel",required = false) String title){
        ResultJson resultJson = new ResultJson(ResultCode.REQUIRED_SUCCESS);
        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageSize == null || pageSize > 100) {
            pageSize = 50;
        }
        if (!StringUtils.isEmpty(title)) {
            // 拼接查询字符串
            title = "%" + title + "%";
        } else {
            // 拼接查询字符串如果为空字符或者null则 置为null
            title = null;
        }
        List<ProblemAnalysisPreviewVO> problemAnalysisPreviewVOS =problemAnalysisService.selectProblemAnalysisPreview(pageNum,pageSize,problemId,title);
        int count = problemAnalysisService.countProblemAnalysisPreview(problemId,title);
        resultJson.addInfo(problemAnalysisPreviewVOS);
        resultJson.addInfo(count);
        return resultJson;
    }

    //根据题目id返回题目的具体信息
    //@LoginRequired
    @PostMapping("/info")
    public ResultJson selectProblemAnalysisInfo(@RequestParam("id") Integer id){
        ResultJson resultJson = new ResultJson();

        ProblemAanalysisPO problemAanalysisPO = problemAnalysisService.selectProblemAnalysisInfo(id);
        String username = problemAanalysisPO.getAuthor();
        UserCustomInfoVO userCustomInfoVO = userCustomInfoService.select(username);

        resultJson.addInfo(userCustomInfoVO);
        resultJson.addInfo(problemAanalysisPO);
        return resultJson;
    }
}
